<?php
namespace Api\Controller;

/** 
 * 视频操作
 * @author Legend. <xcx_legender@qq.com>
	*
	*id/:id  根据id获取视频信息   [ detail ] [ info ]
 	*list/:per/:timestamp  按时间戳排序  获取此时间戳之前的N条数据  获取 推送的视频信息    
	*无per 使用默认  无timestamp 自动取前面的
	*< userinfo >
	*
	*[ 用户关注人 ] 发布 ( 最新 ) > [ 用户关注游戏 ] 发布 ( 最新 ) > 编辑推荐 > 最新 
	*
	*#时间戳分页   不计算total  按 > create_time   limit(n) 来做 
	*
 * @param 
 * @return 
 */

class VideoController extends PublicController 
{


	/** 
	 * 获取视频详细信息
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	
	public function id()
	{
		G('begin');
		$id = I('id', 0, 'intval');
		$vEvent = A('Api/Video', 'Event');

		$ids[] = $id;
		$video = $this->lists_format($ids);
		$video = current($video);
		if (!$video)
		{
			$this->error(self::$API_ERROR['VIDEO'], '视频信息错误');
		}
		// $vEvent->setPlay($id);//加入播放次数 susy<susy.liang@ilongyuan.com.cn>

		$likers = A('Api/Like', 'Event')->getUser($id, 5);

		$data = array(
				'video'=>$video,
				'liker'=>$likers
				);

		//add the comments by susy 2015/5/21
		/*$comment = A('Api/Comments', 'Event')->get($id, 5);
		$data['comments'] = $comment?$comment:array();*/
		$data['comments'] = array();//保留空的给app，结构不变
		//end

		

		
		//end

		//add 推荐视频 取后台推荐的 susy.liang
		//修改为8个 2016-04-14 16:29:01 Legend.Xie  
		//修改为100个里面随机取 liubo 2016-05-20
		$reIds = $vEvent->getRecommend(100, '', $id);
		shuffle($reIds);
		$reIds = array_slice($reIds, 0, 8);
		$data['recommend'] = $this->lists_format($reIds);
		//end
		G('end');
		// echo G('begin', 'end', 6).'s';
		$this->success('', $data);
	}


	public function check()
	{
		$data = I('');
		if (!event('Sensitive')->check($data['title'])){
			$this->error(500, '标题非法');
		}
		if (!event('Sensitive')->check($data['detail'])){
			$this->error(500, '描述非法');
		}
		$this->success('数据合法');
	}



	/** 
	 * 设置播放次数接口
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	
	public function setPlay()
	{
		$id = I('id', 0, 'intval');
		$id > 0 or $this->success('');
		define('PLATFORM', I('platform', 0, 'intval'));
		$platappid = I('appid', 0, 'intval');

		if ($platappid){
			$app = model('Platform')->getByAppId($platappid);
			if (!$app){
				$platappid = 0;
			}
		}

		A('Video', 'Event')->setPlay($id, 1, ['appid' => $platappid]);
		//add 浏览记录 susy.liang
		$appToken = I('appToken');
		if($appToken){
			$uid = $this->getUid($appToken);
			$uid && A('Api/History', 'Event')->add($uid, $id);
		}
		$this->success('');
	}



	/** 
	 * 刷新统计缓存
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	
	public function refresh()
	{
		A('Cache', 'Event')->videoRefresh();
	}

	/**
	*加载视频的评论
	*根据视频id，按时间戳进行分页加载
	*@author susy<susy.liang@ilongyuan.com.cn>
	*/
	public function comment(){

		$comEvent  = A('Api/Comments', 'Event');

		$video = I('id', 0, 'intval');
		$timestamp = I('timestamp', 0, 'intval');
		$pager 	   = I('pager', C('APP_COMMENT_LIST',NULL, 20), 'intval');

		$map = array();
		$map['create_time'] = $timestamp ? array('lt', $timestamp) : array('gt', 0);

		$token = I('request.appToken', '', 'strip_tags,trim');
		$uid = $this->getUid($token);
		$comments = $comEvent->get($video, $pager, '', $map, true, $uid);
		$this->success('', $comments);

	}

	/**
	 * 热门评论
	 * @author liubo <liubo@ilongyuan.com.cn>
	 */
	public function hotComment() {
		$comEvent  = A('Api/Comments', 'Event');

		$video_id = I('id', 0, 'intval');
		$limit = I('limit', 10, 'intval');

		$token = I('request.appToken', '', 'strip_tags,trim');
		$uid = $this->getUid($token);
 		$comments = $comEvent->getHot($video_id, 1, $limit, true, $uid);
		$this->success('', $comments);
	}

 	
 	/** 
 	 * 获取视频下所有的弹幕信息
 	 * @author Legend. <xcx_legender@qq.com>
 	 * @param 
 	 * @return 
 	 */
 	
 	public function barrages()
 	{
 		$video_id = I('id', 0, 'intval');
 		$bEvent = A('Api/Barrage', 'Event');
 		$lists = $bEvent->lists($video_id);
 		foreach ($lists as $key => $value) {
 			unset($value['_id']);
 			$res[] = $value;
 		}
 		$this->success('', $res);
 	}

 	/** 
 	 * [谁点赞过的]
 	 * @author Legend. <xcx_legender@qq.com>
 	 * @param 
 	 * @return 
 	 */
 	public function likers()
 	{
 		$id = I('id', 0, 'intval');

		$video = event('Video')->info($id);
		if(!$video) $this->error(self::$API_ERROR['VIDEO'], '视频信息错误');
		$data = [];
		$total = $video['zan'];
		// if($total){
			$p = I('p', 1, 'intval');
			$pager 	   = I('pager', C('USER_LIST_NUM',NULL, 20), 'intval');
			$data = event('Like')->getUser($id, $pager, $p);
		// }

		$this->list_success($data, intval($total));
 	}

 	/**
 	 * 视频动态
 	 * @author liubo <liubo@ilongyuan.com.cn>
 	 */
 	public function dynamics()
 	{
 		$video_id = I('id', 0, 'intval');
 		!$video_id && $this->param_error();

 		$list = event('video')->dynamics($video_id);
 		$this->success('请求成功', $list);
 	}

 	public function transcode_notify()
 	{
 		$taskId = I('TaskId');
 		$status = I('Status');
 		$data = [
 			'taskId' => I('TaskId'),
 			'status' => I('Status'),
 			'uri' => I('DestFilePath'),
 			'size' => I('DestFileSize'),
 		];
 		event('Video')->finishTrans($data);
 		$this->success('ok');
 	}

    public function source($id = 0, $uri = '', $appid = 0, $from = '') {
        $id = intval($id);
        if ($id) {
            $vEvent = event('video');
            $vEvent->setPlay($id, 1, ['appid' => $appid, 'from' => $from]);
        }
        if ($uri) {
            redirect($uri);
        }
    }

}